//
// Created by steven on 2017/3/9.
// Copyright (c) 2017 steven. All rights reserved.
//

#import "GMGuardDataController.h"
#import "GMGuardRecord.h"
#import "NSDate+Utils.h"


@implementation GMGuardDataController

// Get unused guards
- (void)getUnusedGuardsWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    __weak typeof(self)weakSelf = self;
    
    [self requestWithType:POST url:ACTION_GUARDS params:params success:^(id object) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        NSDictionary *metaInfo = object[@"meta"][@"pagination"];
        NSArray *data = object[@"data"];
        strongSelf.unUsedGuardRecord.totalCount = [metaInfo[@"total"] integerValue];
        strongSelf.unUsedGuardRecord.currentPage = [metaInfo[@"current_page"] integerValue];
        strongSelf.unUsedGuardRecord.totalPage = [metaInfo[@"total_pages"] integerValue];
        
        NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        
        [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            GMGuardModel *guardModel = [[GMGuardModel alloc] init];
            guardModel.id = [obj[@"id"] integerValue];
            guardModel.sn = obj[@"sn"];
            guardModel.owner = obj[@"owner"];
            guardModel.status = obj[@"status"];
            guardModel.useTime = obj[@"useTime"];
            guardModel.usage = obj[@"usedFor"];
            guardModel.checkingTime = [NSDate dateFromString: obj[@"checkingTime"]];
            
            [tempArray addObject:guardModel];
        }];
        
        if (strongSelf.unUsedGuardRecord.currentPage == 1) {
            [strongSelf.unUsedGuardRecord.guards removeAllObjects];
        }
        
        [strongSelf.unUsedGuardRecord.guards addObjectsFromArray:tempArray];
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"获取守卫失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case 500:
                errorString = @"创建token时出错";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];

}

// Get used guards
- (void)getUsedGuardsWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    __weak typeof(self)weakSelf = self;
    
    [self requestWithType:POST url:ACTION_GUARDS params:params success:^(id object) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        NSDictionary *metaInfo = object[@"meta"][@"pagination"];
        NSArray *data = object[@"data"];
        strongSelf.usedGuardRecord.totalCount = [metaInfo[@"total"] integerValue];
        strongSelf.usedGuardRecord.currentPage = [metaInfo[@"current_page"] integerValue];
        strongSelf.usedGuardRecord.totalPage = [metaInfo[@"total_pages"] integerValue];
        
        NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        
        [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            GMGuardModel *guardModel = [[GMGuardModel alloc] init];
            guardModel.id = [obj[@"id"] integerValue];
            guardModel.sn = obj[@"sn"];
            guardModel.owner = obj[@"owner"];
            guardModel.status = obj[@"status"];
            guardModel.useTime = obj[@"useTime"];
            guardModel.usage = obj[@"usedFor"];
            guardModel.checkingTime = [NSDate dateFromString: obj[@"checkingTime"]];
            
            [tempArray addObject:guardModel];
        }];
        
        if (strongSelf.usedGuardRecord.currentPage == 1) {
            [strongSelf.usedGuardRecord.guards removeAllObjects];
        }
        
        [strongSelf.usedGuardRecord.guards addObjectsFromArray:tempArray];
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"获取守卫失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case 500:
                errorString = @"创建token时出错";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];

}

// Get checking guards
- (void)getCheckingGuardsWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    __weak typeof(self)weakSelf = self;
    
    [self requestWithType:POST url:ACTION_GUARDS params:params success:^(id object) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        NSDictionary *metaInfo = object[@"meta"][@"pagination"];
        NSArray *data = object[@"data"];
        strongSelf.checkingGuardRecord.totalCount = [metaInfo[@"total"] integerValue];
        strongSelf.checkingGuardRecord.currentPage = [metaInfo[@"current_page"] integerValue];
        strongSelf.checkingGuardRecord.totalPage = [metaInfo[@"total_pages"] integerValue];
        
        NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        
        [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            GMGuardModel *guardModel = [[GMGuardModel alloc] init];
            guardModel.id = [obj[@"id"] integerValue];
            guardModel.sn = obj[@"sn"];
            guardModel.owner = obj[@"owner"];
            guardModel.status = obj[@"status"];
            guardModel.useTime = obj[@"useTime"];
            guardModel.usage = obj[@"usedFor"];
            guardModel.checkingTime = [NSDate dateFromString: obj[@"checkingTime"]];
            
            [tempArray addObject:guardModel];
        }];
        
        if (strongSelf.checkingGuardRecord.currentPage == 1) {
            [strongSelf.checkingGuardRecord.guards removeAllObjects];
        }
        
        [strongSelf.checkingGuardRecord.guards addObjectsFromArray:tempArray];
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"获取守卫失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case 500:
                errorString = @"创建token时出错";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];

}

- (GMGuardRecord *)unUsedGuardRecord {
    if (!_unUsedGuardRecord) {
        _unUsedGuardRecord = [[GMGuardRecord alloc] init];
    }
    return _unUsedGuardRecord;
}

- (GMGuardRecord *)usedGuardRecord {
    if (!_usedGuardRecord) {
        _usedGuardRecord = [[GMGuardRecord alloc] init];
    }
    return _usedGuardRecord;
}

- (GMGuardRecord *)checkingGuardRecord {
    if (!_checkingGuardRecord) {
        _checkingGuardRecord = [[GMGuardRecord alloc] init];
    }
    return _checkingGuardRecord;
}

@end
